package com.intel.factorytest.activity;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.intel.factorytest.R;
public class ProximitySensor extends Activity implements SensorEventListener {
    private GLSurfaceView mGLSurfaceView;
    private SensorManager mSensorManager;
    private float[] mGData = new float[3];
    private float[] mMData = new float[3];
    private float[] mR = new float[16];
    private float[] mI = new float[16];
    private FloatBuffer mVertexBuffer;
    private FloatBuffer mColorBuffer;
    private ByteBuffer mIndexBuffer;
    private float[] mOrientation = new float[3];
    private int mCount;
    private int OrientationEnable;

    private TextView mInfo ;
    private String mDistance="";
    private float gx1 = 0;
    private float gy1 = 1;
    private float gz1 = 2;
    private float gx = 0;
    private float gy = 1;
    private float gz = 2;
    private int count = 0;
    private String tmp = null;  
    private static final String TEST_ITEM = "test_item";
    private static final String TEST_ITEM_RESULT = "test_item_result";
    private boolean is_pass;
    private static final String TEST_RESULT = "test_result";

    /** Called with the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String orientationStr = intent.getStringExtra("sd_orientation");
    int orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    Log.i("util","orientationStr = " + orientationStr);
    if(orientationStr.equals("SCREEN_ORIENTATION_PORTRAIT")) {
    orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    super.setRequestedOrientation(orientation);

    } else if(orientationStr.equals("SCREEN_ORIENTATION_LANDSCAPE")) {
    orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    super.setRequestedOrientation(orientation);

    }
        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        mInfo = new TextView(this);
    mInfo.setTextSize(24.0f);
        mInfo.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
        mInfo.setGravity(Gravity.CENTER);
        setContentView(mInfo);
    }

    @Override
    protected void onResume() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onResume();
        mDistance = getResources().getString(R.string.SERVICEMENU_PROXIMITY_DISTANCE_TXT);
        Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    Log.i("yjc","msensor:" + msensor);
    if(msensor == null) {
        mInfo.setText(getString(R.string.SERVICEMENU_APROXIMITY_FAILED_TXT));
        is_pass = false;
    }
    
        mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_GAME);
        //Because the OpenGL have issue in  mode from portrait to Landscape.
        /*OrientationEnable = Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
        Log.i("OrientationEnable", ""+OrientationEnable);
        if (1 == OrientationEnable)
        {
            Settings.System.putInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION,0 );
        }*/
    }

    @Override
    protected void onPause() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onPause();
        mSensorManager.unregisterListener(this);
    } 

   
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) {
        int type = event.sensor.getType();
    
        if (type == Sensor.TYPE_PROXIMITY) {
        gx = event.values[0];//distance
        gy = event.values[1];
        gz = event.values[2];
        tmp = mDistance+gx+"\n";
        if (gx!=gx1 || gy!=gy1 || gz!=gz1) {
            tmp = tmp +"\n"+getString(R.string.SERVICEMENU_APROXIMITY_OK_TXT);
            is_pass = true;
    //        finish();
        }else {
            //is_pass = false;
    //        finish();
        }
    
        mInfo.setText(tmp);
        }
    }

    
    @Override
    protected Dialog onCreateDialog(int none)
    {        
        return new AlertDialog.Builder(this)
//        .setIcon(R.drawable.alert_dialog_icon)
        .setTitle(R.string.SERVICEMENU_DIALOG_TITLE_TXT)
        .setPositiveButton(R.string.SERVICEMENU_DIALOG_YES_TXT, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked OK so do some stuff */
                finish();
            }
        })
        .setNegativeButton(R.string.SERVICEMENU_DIALOG_NO_TXT, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked Cancel so do some stuff */
                finish();
            }
        })
        .create();
    }
    
    private void setTestResult () {
        Intent intent = new Intent();
        Intent mIntent = getIntent();
        int pos = mIntent.getIntExtra(TEST_ITEM,-1);
        intent.putExtra(TEST_ITEM,pos);
        intent.putExtra(TEST_ITEM_RESULT,is_pass);
//        Log.e(TAG,"pos ="+pos);
        setResult(0, intent);
    }
    @Override
    public void finish() {
        setTestResult (); 
        super.finish();
    }
    public boolean ac_ispass() {
        return is_pass;
    } 
}
